package com.admin.application;

import com.admin.domain.modle.Play;
import com.admin.domain.repository.PlayRepository;
import com.admin.infrastructure.RemoteDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * description User: WeiDa Date: 2017-4-11 Time: 16:45
 */
@Service
public class PlayService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(PlayService.class);
	@Autowired
	protected PlayRepository playRepository;
	@Autowired
	private ThreadPoolTaskExecutor taskExecutor;

	public Play create(Play play) {
		play.setPlayId(UUID.randomUUID().toString());
		playRepository.add(play);
		return play;
	}

	public Play modify(Play play) {
		playRepository.update(play);
		return play;
	}

	public Play get(int id) {
		return playRepository.get(id);
	}
	
	public Play get(String id) {
		return playRepository.get(id);
	}

	public List<Play> list() {
		return this.list(null);
	}

	@Transactional
	public List<Play> list(String key) {
		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				List<Play> plays = RemoteDataUtil.postHaiyinShowList();
				if (plays != null) {
					playRepository.upsert(plays);
				}
			}
		});

		return playRepository.list(key);
	}

	public void delete(String code) {
		playRepository.remove(code);
	}

	public List<Play> list2(String key) {
		return playRepository.list(key);
	}

    public void updateSaleDateById(String playId, String  saleDate) {
    }
}
